package ks.client.controllers.commands;

import org.w3c.dom.Document;

import ks.client.kommander.Action;
import ks.client.lobby.Lobby;
import ks.framework.common.Message;

public class AccountCommandController {
	private Lobby lobby;
	
	public AccountCommandController(Lobby l) {
		this.lobby = l;
	}
	
	/**
	 * Just send account.
	 * 
	 * @param player
	 * @param oldPwd
	 * @param newPwd Pass null if you don't want to send this.
	 * @param icon Pass null if you don't want to send this.
	 */
	public void send(String player, String oldPwd, String newPwd, String icon) {
		String uid = lobby.getKommander().generateUid();
		
		StringBuilder sb = new StringBuilder("<command version=\"1.0\" id=\"" + uid + "\">");
		sb.append("<account player=\"" + player + "\" old-password=\"" + oldPwd + "\"");
			sb.append(" password=\"" + newPwd + "\"");
			sb.append(" icon=\"" + icon + "\"/>");
		sb.append("</command>");
		System.out.println("Sending this command" + sb);
		Document d = Message.construct(sb.toString());
		Message m = new Message(d);
		lobby.getUserContext().getClient().sendToServer(m);
	}
	
	/**
	 * Send account and perform an Action on ack/nack.
	 * 
	 * @param player
	 * @param oldPwd
	 * @param newPwd Pass null if you don't want to send this.
	 * @param icon Pass null if you don't want to send this.
	 * @param action
	 */
	public void send(String player, String oldPwd, String newPwd, String icon, Action action) {
		String uid = lobby.getKommander().generateUid();
		
		StringBuilder sb = new StringBuilder("<command version=\"1.0\" id=\"" + uid + "\">");
		sb.append("<account player=\"" + player + "\" old-password=\"" + oldPwd + "\"");
		if (newPwd != null)
			sb.append(" password=\"" + newPwd + "\"");
		if (icon != null)
			sb.append(" icon=\"" + icon + "\"/>");
		sb.append("</command>");
		
		lobby.getKommander().addAction(uid, action);
		
		Document d = Message.construct(sb.toString());
		Message m = new Message(d);
		lobby.getUserContext().getClient().sendToServer(m);
	}
}